package com.qing.cheng.service.impl;


import com.qing.cheng.constant.SysConstant;
import com.qing.cheng.dto.OrderDto;
import com.qing.cheng.dto.QcCourseDto;
import com.qing.cheng.entity.QcOrder;
import com.qing.cheng.mapper.QcCourseMapper;
import com.qing.cheng.mapper.QcOrderMapper;
import com.qing.cheng.service.OrderService;
import com.qing.cheng.util.SessionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired(required = false)
    private QcOrderMapper qcOrderMapper;
    @Autowired(required = false)
    private QcCourseMapper qcCourseMapper;

    /**
     * 代支付
     */
    private static final String WAIT_PAY = "0";

    /**
     * 已支付
     */
    private static final String PAID = "1";


    /**
     * 已取消
     */
    private static final String CANCEL = "10";




    @Override
    public void generateNewOrder(QcOrder qcOrder, HttpServletRequest request) {
        Date date = new Date();
        qcOrder.setUserId(SessionUtil.getUserId(request));
        qcOrder.setUpdatedDate(date);
        qcOrder.setCreatedDate(date);
        qcOrder.setUpdatedBy(SysConstant.SYSTEM_NAME);
        qcOrder.setCreatedBy(SysConstant.SYSTEM_NAME);
        qcOrder.setStatus(WAIT_PAY);
        qcOrderMapper.insertSelective(qcOrder);
    }

    @Override
    public List<OrderDto> listOrder(String status, HttpServletRequest request) {
        Integer userId = SessionUtil.getUserId(request);
        List<QcOrder> qcOrders = qcOrderMapper.listAllByUserId(userId,status);
        List<OrderDto> orderDtoList = new ArrayList<>();
        for (QcOrder order : qcOrders) {
            OrderDto orderDto = new OrderDto();
            BeanUtils.copyProperties(order,orderDto);
            QcCourseDto courseDto = qcCourseMapper.getOneById(order.getCourseId());
            orderDto.setQcCourse(courseDto);
            orderDtoList.add(orderDto);
        }
        return orderDtoList;
    }


}
